//
//  SEDepartmentListViewController.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/12.
//

import UIKit

class SEDepartmentListViewController: SEBaseViewController {
    
    private var viewModel = SEDepartmentViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewModel.updateData = { [weak self] status in
            self?.endRefreshing()
            switch status {
            case .success:
                self?.collectionView.reloadData()
            case .auth:
                self?.presentLoginViewController(selector: #selector(self?.loadData))
            case .failure:
                break
            }
        }
    }
    
    override func setupUI() {
        super.setupUI()
        
        collectionView.se_header = SERefreshHeader { [weak self] in self?.loadData() }
        collectionView.se_header?.beginRefreshing()
    }

    // MARK: - getter
    
    @IBOutlet private weak var collectionView: UICollectionView! {
        didSet {
            collectionView.backgroundColor = .background
            collectionView.register(SEDepartmentListCell.nibForCell(),
                                    forCellWithReuseIdentifier: SEDepartmentListCell.reuseIdentifier())
        }
    }
}

extension SEDepartmentListViewController {
    @objc private func loadData() {
        viewModel.getListOfAllDepartments()
    }
    
    private func endRefreshing() {
        collectionView.endHeaderRefreshing()
    }
}

// MARK: - UICollectionViewDataSource

extension SEDepartmentListViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.numberOfItems
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEDepartmentListCell.reuseIdentifier(),
                                                      for: indexPath) as! SEDepartmentListCell
        let vm = viewModel.viewModel(at: indexPath)
        cell.update(with: vm)
        return cell
    }
}

// MARK: - UICollectionViewDelegate & UICollectionViewDelegateFlowLayout

extension SEDepartmentListViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return viewModel.viewModel(at: indexPath).itemSize
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vm = viewModel.viewModel(at: indexPath)
        pushToDoctorListVc(vm)
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? SEDepartmentListCell else { return }
        cell.setSelectedHighlight(true)
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? SEDepartmentListCell else { return }
        cell.setSelectedHighlight(false)
    }
}

// MARK: - push action event

extension SEDepartmentListViewController {
    /// 跳转 医生 列表
    private func pushToDoctorListVc(_ vm: SEDepartmentItemViewModel) {
        let doctorListVc = SEDoctorListViewController(fromType: .department,
                                                      departmentId: vm.model.departmentId ?? "")
        navigationController?.pushViewController(doctorListVc, animated: true)
    }
}
